<template>
  <div class="app-container">
    <!-- 表格-条件筛区域  -->
    <div class="filter-container">
      <el-input placeholder="请输入昵称" v-model="searchParam.SEARCH_EQ_username" style="width: 200px;" class="filter-item" clearable @keyup.enter.native="handleFilter"/>
      <el-input placeholder="请输入真实姓名" v-model="searchParam.SEARCH_EQ_real_name" style="width: 200px;" class="filter-item" clearable @keyup.enter.native="handleFilter"/>
      <el-input placeholder="请输入手机号" v-model="searchParam.SEARCH_EQ_mobile" style="width: 200px;" class="filter-item" clearable @keyup.enter.native="handleFilter"/>
      <el-select placeholder="请选择商品类型" v-model="searchParam.SEARCH_EQ_type" style="width: 200px;" class="filter-item" clearable >
          <el-option label="课程" :value="1"></el-option>
          <el-option label="考试" :value="2"></el-option>
      </el-select>
      <el-input placeholder="请输入订单编号" v-model="searchParam.SEARCH_LIKE_order_num" style="width: 200px;" class="filter-item" clearable @keyup.enter.native="handleFilter"/>
      <el-input placeholder="请输入商品名称" v-model="searchParam.SEARCH_LIKE_good_name" style="width: 200px;" class="filter-item" clearable @keyup.enter.native="handleFilter"/>
      <el-select placeholder="请选择开票状态" v-model="searchParam.SEARCH_EQ_order_invoice_status" style="width: 200px;" class="filter-item" clearable  >
        <el-option value="0" label="未开票" ></el-option>
        <el-option value="1" label="待开票"></el-option>
        <el-option value="2" label="已开票"></el-option>
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter"> 搜索 </el-button>
<!--      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus" @click="handleCreate"> 新增 </el-button>-->
    </div>

    <!-- 表格-内容区域 -->
    <el-table v-loading="listLoading" :key="tableKey" :data="tableList" border fit highlight-current-row style="width: 100%;">
      <el-table-column v-for="column in columns" :key="column.prop"
                       :prop="column.prop"
                       :label="column.label"
                       :width="column.width"
                       :align="column.align?column.align:'center'"
                       :min-width="column.minWidth"
                       :sortable="column.sortable"
                       :fixed="column.fixed"
                       :type="column.type" :formatter="column.formatter"> ]]rrrrrrr
        <template slot-scope="scope">
          <!-- 行内显示图片 -->
          <img v-if="column.imgFlag==true" :src=" scope.row[column.prop].indexOf('https://') >-1 ?  scope.row[column.prop] : ( CMBASE.baseFileContext + scope.row[column.prop] ) " width="60" height="60" class="img"/>
          <!-- 行内显示开关按钮 -->
          <el-switch v-else-if="column.switchFlag==true" v-model="scope.row[column.prop]" :active-value="1" :inactive-value="0"  disabled />
          <!-- 行内显示tag标签 -->
          <span  v-else-if="column.tagFlag==true">
                        <span v-for="item in column.tagFormatter" :key="item.value" >
                            <el-tag v-if="item.value==scope.row[column.prop]"   :type="item.type">{{item.text}}</el-tag>
                        </span>
                    </span>
          <!-- 行内格式自定义 -->
          <span v-else-if="column.formatter"> {{column.formatter(scope.row)}}</span>
          <!--默认显示文本-->
          <span v-else>{{scope.row[column.prop]}}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="240" class-name="small-padding" fixed="right" >
        <template slot-scope="scope">
          <el-button type="text"  icon="el-icon-edit" class="el-button--primary"  @click="handleUpdate(scope.row)"> 查看详情 </el-button>
          <el-button type="text" v-if="scope.row.orderInvoiceStatus===1"  icon="el-icon-edit" class="el-button--primary"  @click="handleInvoice(scope.row)"> 发票申请 </el-button>
          <el-button type="text" icon="el-icon-refresh-right" v-if="scope.row.type===1" class="el-button--primary" @click="refresh(scope.row)">课程退款</el-button>
<!--          <el-button type="text"  icon="el-icon-delete"   class="el-button&#45;&#45;danger"  @click="handleDelete(scope.row)"> 删除 </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <!-- 表格-页脚区域 -->
    <pagination v-show="tableTotal>0" :total="tableTotal" :page.sync="pageRequest.pageNum" :limit.sync="pageRequest.pageSize" @pagination="getList"/>

    <!--新增编辑界面-->
    <el-dialog :title="operation?'新增':'编辑'" width="45%" top="5vh" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form :model="dataForm" label-width="140px" :rules="dataFormRules" ref="dataForm" style="width: 80%">


        <el-form-item label="商品名称" prop="goodName">
          <el-input v-model="dataForm.goodName" auto-complete="off" placeholder="请输入商品名称"/>
        </el-form-item>
        <el-form-item label="订单状态" prop="status">
          <el-select v-model="dataForm.status">
            <el-option :value="0" label="待支付"></el-option>
            <el-option :value="1" label="已支付"></el-option>
            <el-option :value="2" label="已取消"></el-option>
            <el-option :value="3" label="已退款"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付金额" prop="payAmount">
          <el-input v-model="dataForm.payAmount" auto-complete="off" placeholder="请输入支付金额"/>
        </el-form-item>
        <el-form-item label="订单金额" prop="orderAmount">
          <el-input v-model="dataForm.orderAmount" auto-complete="off" placeholder="请输入订单金额"/>
        </el-form-item>
        <el-form-item label="商品类型" prop="type">
          <el-select v-model="dataForm.type">
            <el-option :value="1" label="课程"></el-option>
            <el-option :value="2" label="考试"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付类型" prop="payType">
          <el-select v-model="dataForm.payType">
            <el-option :value="2" label="微信H5"></el-option>
            <el-option :value="3" label="微信Native"></el-option>
            <el-option :value="6" label="支付宝PC"></el-option>
            <el-option :value="7" label="支付宝手机"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-input v-model="dataForm.payTime" auto-complete="off" placeholder="请输入支付时间"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click.native="closeDialog()">取消</el-button>
<!--        <el-button size="mini" @click.native="saveFormData()" :loading="editLoading" type="primary">提交 </el-button>-->
      </div>
    </el-dialog>

<!--    <el-dialog-->
<!--        text="发票申请"-->
<!--        :visible.sync="ShowInvoice"-->
<!--        width="75%"-->
<!--        top="5vh"-->
<!--        :close-on-click-modal="false"-->
<!--    >-->
<!--      <Invoice v-if="ShowInvoice" :pid="pid"  ></Invoice>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button size="mini" @click.native="closeInvoiceDialog()">取消</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->


    <el-dialog text="发票申请"
               :visible.sync="ShowInvoice"
               width="75%"
               top="5vh"
               :close-on-click-modal="false">
      <el-form :model="dialogDataForm" label-width="140px"  ref="dialogDataForm" style="width: 80%">
        <el-form-item label="发票类型" prop="invoiceType">
          <el-select v-model="dialogDataForm.invoiceType">
            <el-option :value="1" :label="'普通电子'"></el-option>
            <el-option :value="2" :label="'增值税专用'"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请类型" prop="applyType">
          <el-select v-model="dialogDataForm.applyType">
            <el-option :value="1" :label="'个人'"></el-option>
            <el-option :value="2" :label="'企业'"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="纳税人识别码" prop="taxerCode">
          <el-input v-model="dialogDataForm.taxerCode" auto-complete="off" placeholder="请输入纳税人识别码"/>
        </el-form-item>
        <el-form-item label="单位名称" prop="applyName">
          <el-input v-model="dialogDataForm.applyName" auto-complete="off" placeholder="请输入单位名称"/>
        </el-form-item>
        <el-form-item label="发票抬头" prop="invoiceHead">
          <el-input v-model="dialogDataForm.invoiceHead" auto-complete="off" placeholder="请输入发票抬头"/>
        </el-form-item>
        <el-form-item label="收件邮箱" prop="receiveMail">
          <el-input v-model="dialogDataForm.receiveMail" auto-complete="off" placeholder="请输入收件邮箱"/>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-input v-model="dialogDataForm.createTime" auto-complete="off" placeholder="请输入创建时间"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click.native="closeInvoiceDialog()">取消</el-button>
        <el-button size="mini" @click.native="handlePass()" :loading="editLoading" type="primary">开票 </el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 分页栏信息
import Invoice from './invoice.vue'
export default {
  name: 'order',
  components: {
    Pagination,
    Invoice,
  },
  directives: {   },
  filters: { },
  data() {
    return {
      tableKey: 0,
      tableList: [],//表格数据源
      tableTotal: 0,//表格总条数数据
      listLoading: true,
      pageRequest: { pageNum: 1, pageSize: 10, },
      searchParam:{},
      orderParam:{order_invoice_status:false,create_time:false, },
      dialogFormVisible: false,//默认不显示弹出框
      operation: true, // true:新增, false:编辑
      editLoading: false,//保存表单时loading框
      ShowInvoice: false,//保存表单时loading框
      columns: [
        {prop:'username',label: '昵称',sortable: false ,width:120,fixed:true},
        {prop:'realName',label: '真实姓名',sortable: false ,width:120,fixed:true},
        {prop:'mobile',label: '手机号',sortable: false,width:120 ,fixed:true},
        {prop: 'orderNum',  label: '订单编号',  sortable:false ,width:120 },
        {prop: 'type',  label: '商品类型',  sortable:false ,tagFlag: true ,width:120 ,
          tagFormatter: [
            {text: '课程', value: '1' , type: 'success'},
            {text: '考试', value: '2' , type: 'primary'},
          ]
        },
        {prop: 'goodName',  label: '商品名称',  sortable:false ,width:120 },
        {prop: 'goodImg',  label: '商品图片',  sortable:false ,imgFlag:true,width:120 },
        {prop: 'payAmount',  label: '支付金额',  sortable:false ,width:120 },
        {prop: 'status',  label: '订单状态',  sortable:false ,tagFlag:true ,width:120,
          tagFormatter:[
            {text: '待支付', value:'0',type:'info'},
            {text: '已支付', value:'1',type:'primary'},
            {text: '已取消', value:'2',type:'warning'},
            {text: '已退款', value:'3',type:'danger'},
          ]
        },
        {prop: 'createTime',  label: '下单时间',  sortable:false  ,width:200},
        {prop: 'payTime',  label: '支付时间',  sortable:false  ,width:200},
        {prop: 'orderAmount',  label: '订单金额',  sortable:false  ,width:120},
        {prop: 'payType',  label: '支付类型',  sortable:false ,tagFlag: true ,width:120,
          tagFormatter: [
            {text: '微信Native' , value: '3' , type: 'success'},
            {text: '微信H5' , value: '2' , type: 'primary'},
            {text: '支付宝PC' , value: '6' , type: 'primary'},
            {text: '支付宝手机' , value: '7' , type: 'primary'},
          ]
        },
        {prop: 'thirdNum',  label: '支付订单号',  sortable:false ,width:120 },
        {prop: 'orderInvoiceStatus',  label: '发票状态',  sortable:false ,tagFlag: true ,width:120 ,
          tagFormatter: [
            {text: '未申请' , value: '0' , type: 'success' },
            {text: '待开票' , value: '1' , type: 'warning' },
            {text: '已开票' , value: '2' , type: 'danger' },
          ]
        },
      ],
      fileList: [],
      // 新增编辑界面数据
      dataForm: {},
      dialogDataForm: {},
      //表单检验规则
      dataFormRules: {
      },
      pid:'',
    }
  },
  created() {
    this.getList();
  },
  methods: {
    //获取表格数据
    getList() {
      this.listLoading = true
      //【API请求】----获取表格列表数据
      this.pageRequest.orderParam=this.orderParam
      this.$api.getSOrderList(this.pageRequest).then(response => {
        this.tableList = response.data.content
        this.tableTotal = response.data.total
        this.listLoading = false;
      }).catch((res) => {
        this.$message({  message: '列表获取失败', type: 'error' })
      })
    },
    //表格数据筛选
    handleFilter() {
        this.pageRequest = {
            pageNum: 1,
            pageSize: 10,
        }
      this.pageRequest.searchParam = this.searchParam
      this.getList()
    },
    //【按钮点击】--- 弹出新增表单
    handleCreate() {
      this.operation = true;
      this.initDataForm();
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
        this.editLoading = false;
      })
    },
    //【按钮点击】--- 弹出编辑表单
    handleUpdate(row) {
      this.operation = false;
      this.dataForm = Object.assign({}, row)
      //显示编辑菜单
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
        this.editLoading = false;
      })
    },
    //保存表单数据
    saveFormData() {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false;
        }
        this.editLoading = true;
        //【API请求】----保存数据
        this.$api.saveSOrder(this.dataForm).then(() => {
          this.editLoading = false;//关闭loading框
          this.dialogFormVisible = false;//关闭表单页面
          this.initDataForm();//重置表单数据
          this.getList();//重新获取表格数据
          this.$notify({ title: '成功',  message: '保存成功', type: 'success', duration: 2000 })
        })
      })
    },
    //【按钮点击】---删除行
    handleDelete(row) {
      this.$confirm('确认删除该记录吗？', '提示', {}).then(() => {
        //【API请求】---- 删除数据
        this.$api.deleteSOrder({ids:row.id}).then(() => {
          this.$message({  message: '操作成功', type: 'success' })
            this.pageRequest.pageNum = 1
          this.getList();
        })
      }).catch(() => {
        this.$message({  message: '操作失败', type: 'error' })
      });
    },
    //初始化表单数据，重新渲染
    initDataForm(){
      this.dataForm = {
        delFlag: '' ,
        status: '' ,
        memberId: '' ,
        goodImg: '' ,
        payAmount: '' ,
        goodId: '' ,
        orderAmount: '' ,
        type: '' ,
        id: '' ,
        payType: '' ,
        payTime: '' ,
        thirdNum: '' ,
        goodName: '' ,
        orderNum: '' ,
        orderInvoiceStatus: '' ,
      };
      this.dialogDataForm={
        invoiceType: '' ,
        applyType: '' ,
        taxerCode: '' ,
        id: '' ,
        memberId: '' ,
        applyName: '' ,
        invoiceHead: '' ,
        receiveMail: '' ,
        createTime: '' ,
        orderId: '' ,
      };
    },
    //关闭表单弹出框
    closeDialog(){
      this.$refs['dataForm'].resetFields();
      this.dialogFormVisible = false
    },

    //发票申请
    handleInvoice(data){
      this.pid = data.id
      this.ShowInvoice=true
      this.$api.getCMemberInvoiceList({id:this.pid}).then(response => {
        this.dialogDataForm = Object.assign({}, response.data)
      })
    },


    closeInvoiceDialog(){
      this.ShowInvoice = false
      this.getList();
    },

    handlePass(){
      this.$api.passInvoice({id:this.pid}).then(
          res=>{
            if (res.code===0){
              this.$message({message:'开票成功',type:'success'})
              this.closeInvoiceDialog();
              this.getList();
            }else{
              this.$message({message:'开票失败',type:'error'})
              this.closeInvoiceDialog();
              this.getList();
            }
          }
      )
    },

    refresh(row){
      this.$confirm('是否给该用户退款？', '提示', {}).then( ()=>{
        this.$api.refundMethod({id:row.id}).then(res=>{
          console.log(res)
          this.$message({message:'退款成功',type:'success'})
        })
      })
    },

  }


}
</script>
